#encoding=utf-8
'''
Created on 2014-12-9

@author: cooler
'''
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import  logging

# send email
import smtplib
from email.mime.text import MIMEText
from email.Header import Header
from email.MIMEMultipart import MIMEMultipart

logger = logging.getLogger('Email')
logger.setLevel(logging.DEBUG)


class Monitor(object):
    """docstring for Monitor"""
    #####################
    # 设置服务器，用户名、口令以及邮箱的后缀
    ######################
    mail_host="smtp.163.com"
    mail_user="safeisland"
    mail_pass="19861217"
    mail_postfix="163.com"
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"

    def __init__(self):
        super(Monitor, self).__init__()
    
    #收到任务两分钟内，redis未增加任务的具体内容，发出警告
    def task_monitor(self,mtype=0,content=""):
        to_list= ['zhoupeng@ofo.com']
        subject = u'task_monitor'
        msg = MIMEText(content,'html', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = self.me
        msg['To'] = ";".join(to_list)
        try:
            s = smtplib.SMTP()
            s.connect(self.mail_host)
            s.login(self.mail_user,self.mail_pass)
            s.sendmail(self.me, to_list, msg.as_string())
            s.close()
            return True
        except Exception, e:
            # print str(e)
            return False

    @classmethod
    def heart_beat_send(self,content=""):
        to_list= ['zhoupeng@ofo.com']
        subject = u'tickets_monitor'
        content = u'{0} all is ok '.format(content)
        msg = MIMEText(content,'html', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = self.me
        msg['To'] = ";".join(to_list)
        try:
            s = smtplib.SMTP()
            s.connect(self.mail_host)
            s.login(self.mail_user,self.mail_pass)
            s.sendmail(self.me, to_list, msg.as_string())
            s.close()
            return True
        except Exception, e:
            # print str(e)
            return False
    
    @staticmethod
    def send(to_list,subject,content,**args):
        if args.get("mail_host","")=="":
            mail_host=u"smtp.163.com"
            mail_user=u"safeisland"
            mail_pass=u"19861217"
            mail_postfix=u"163.com"
        else:
            mail_host=args.get("mail_host")
            mail_user=args.get("mail_user")
            mail_pass=args.get("mail_pass")
            mail_postfix=args.get("mail_postfix")
        me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
        msg = MIMEText(content,'html', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = me
        msg['To'] = ";".join(to_list)
        try:
            s = smtplib.SMTP()
            s.connect(mail_host)
            s.login(mail_user,mail_pass)
            s.sendmail(me, to_list, msg.as_string())
            s.close()
            return True
        except Exception, e:
            # print str(e)
            return False


if __name__ == '__main__':
    # m = Monitor()
    # to_list= ['zhoupeng@ofo.com']
    # subject = u'task_monitor'
    # content = u'all is ok test'
    # Monitor.send(to_list,subject,content)
    Monitor.heart_beat_send()




